layui.define(['jquery', 'table', 'layer', 'form', 'laydate', 'common'], function (exports) {
    var $ = layui.jquery,
        layer = layui.layer,
        form = layui.form,
        common = layui.common,
        laydate = layui.laydate,
        table = layui.table;
    var table_fans = null;
    //页面事件列表
    var active = {
        reload: function () {
            //执行重载
            table.reload('table_fans', {
                page: {
                    curr: 1
                }
                , where: {

                }
            });
        },
        remove: function (ids) {
            common.ajaxRemove(prefix + "/remove", ids, function (res) {
                active['reload'].call(this);
            });
        }
    };
    var _config = null;
    var view = {
        init: function (config) {
            _config = config;
            laydate.render({
                elem: '.ss-laydate' //指定元素
            });
            this.initTable();
            this.initForm();
        },
        initTable: function () {
            table_fans = table.render({
                elem: '#table_fans',
                method: 'POST',
                url: prefix + "/listData", //数据接口
                toolbar: '#table_fans_toolbar',
                done: function () {
                    $('#table_fans').next().css('height', 'auto');
                },
                limit: 20,
                height: 680,
                page: true,
                cols: new Array([
                    {
                        type: 'checkbox'
                    },
                    {
                        type: 'numbers',
                        title: '序号',
                        width:50},
                    {
                        field: 'nicknameStr',
                        title: '昵称',
                        width: 150,
                        align: 'center'},
                    {
                        field: 'headimgurl',
                        title: '头像',
                        width: 80,
                        align: 'center',
                        templet:function (d) {
                            return "<img class='fans-portrait' src=" + d.headimgurl + " />"
                        }
                    },
                    {
                        field: 'gender',
                        title: '性别',
                        width: 70,
                        align: 'center',
                        templet:function (d) {
                            return d.gender == 1 ? '男' : '女';
                        }
                    },
                    {
                        field: 'city',
                        title: '省/市',
                        width: 150,
                        align: 'center',
                        templet:function (d) {
                            return (d.province + '-' + d.city);
                        }
                    },
                    {
                        field: 'subscribeTime',
                        title: '关注时间',
                        width: 180,
                        align: 'center'
                    },
                    {
                        field: 'lock',
                        title: '操作',
                        width: 300,
                        toolbar: '#col_operation',
                        align: 'center'
                    }
                    ])
            });


            //表格工具栏按钮事件
            table.on('toolbar(table_fans)', function (obj) {
                switch (obj.event) {
                    case 'fans_sync':
                        layer.confirm('确认同步粉丝？', {
                            icon: 7,
                            title: "提示",
                            btn: ['确认', '取消'] //按钮
                        }, function () {
                            $.ajax({
                                url: '/wxcmsAccountFans/syncAccountFansList',
                                success: function (result) {
                                    if (result.code==0) {
                                        layer.msg("同步成功");
                                        this.refreshTable();
                                    }
                                    layer.msg("同步异常");
                                },
                                error: function () {
                                    layer.msg("同步异常");
                                }
                            })
                        }, function () {
                            layer.closeAll();
                        });
                        break;

                }
            });
            //表格事件
            table.on('tool(table_fans)', function (obj) {
                switch (obj.event) {
                    case 'sync'://同步粉丝
                        $.ajax({
                            url: '/wxcmsAccountFans/syncAccountFans',
                            data:{openId:data.openId},
                            success: function (result) {
                                if (result.success) {
                                    showDialog({
                                        title: '粉丝详细信息'
                                        , template: 'fansInfo'
                                        , saveUrl: ''
                                        , width: 400
                                        , height: 500
                                        , htmlData: result.data
                                        , btn: ["关闭"]
                                        , yes: function (index, layero) {
                                            layer.close(index);
                                        }
                                    })
                                }
                            },
                            error: function () {
                                layer.msg("同步异常");
                            }
                        })
                        break;
                    case 'mss': //发送消息
                        showDialog({
                            title: '发送消息',
                            template: 'msglist',
                            height:600,
                            tableObj: tableObj,
                            htmlData: data,
                            btn:0
                        })
                        break;

                }
            });

        },
        initForm: function () {
            $('form .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
                return false;
            });
        },
        removeItems: function () {
            active['remove'].call(this);
        },
        refreshTable: function () {
            active['reload'].call(this);
        }
    };
    exports('fans', view);
});